/*
通知栏消息 (测试没通过,还需调试....)
https://pub.flutter-io.cn/packages/flutter_local_notifications
dependencies:
  flutter_local_notifications: ^13.0.0
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

其他:
通知数(app图标)
https://pub.flutter-io.cn/packages/flutter_app_badger

dependencies:
  flutter_app_badger: ^1.5.0

import 'package:flutter_app_badger/flutter_app_badger.dart';

 */
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_flutter.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

abstract class XNotificationUtils{

  static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  static void init() async {

    const AndroidInitializationSettings initializationSettingsAndroid =
    AndroidInitializationSettings('ic_launcher'); // 图标资源名

    final List<DarwinNotificationCategory> darwinNotificationCategories =
    <DarwinNotificationCategory>[
      DarwinNotificationCategory(
        'darwinNotificationCategoryText',
        actions: <DarwinNotificationAction>[
          DarwinNotificationAction.text(
            'text_1',
            'Action 1',
            buttonTitle: 'Send',
            placeholder: 'Placeholder',
          ),
        ],
      ),
      DarwinNotificationCategory(
        'darwinNotificationCategoryPlain',
        actions: <DarwinNotificationAction>[
          DarwinNotificationAction.plain('id_1', 'Action 1'),
          DarwinNotificationAction.plain(
            'id_2',
            'Action 2 (destructive)',
            options: <DarwinNotificationActionOption>{
              DarwinNotificationActionOption.destructive,
            },
          ),
          DarwinNotificationAction.plain(
            'navigationActionId',
            'Action 3 (foreground)',
            options: <DarwinNotificationActionOption>{
              DarwinNotificationActionOption.foreground,
            },
          ),
          DarwinNotificationAction.plain(
            'id_4',
            'Action 4 (auth required)',
            options: <DarwinNotificationActionOption>{
              DarwinNotificationActionOption.authenticationRequired,
            },
          ),
        ],
        options: <DarwinNotificationCategoryOption>{
          DarwinNotificationCategoryOption.hiddenPreviewShowTitle,
        },
      )
    ];

    /// Note: permissions aren't requested here just to demonstrate that can be
    /// done later
    final DarwinInitializationSettings initializationSettingsDarwin =
    DarwinInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      notificationCategories: darwinNotificationCategories,
    );

    final LinuxInitializationSettings initializationSettingsLinux =
    LinuxInitializationSettings(
      defaultActionName: 'Open notification',
      defaultIcon: AssetsLinuxIcon('icons/app_icon.png'),
    );

    final InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsDarwin,
      macOS: initializationSettingsDarwin,
      linux: initializationSettingsLinux,
      //windows: windows.initSettings,
    );

    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
      //onDidReceiveNotificationResponse: selectNotificationStream.add,
      //onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
      onDidReceiveNotificationResponse: (NotificationResponse response) {
        print('用户点击了通知: ${response.payload}'); // 处理通知点击
      },
    );


  }


  static void requestPermissions() async {
    if (XFlutterUtils.isAndroid()) {
      final bool granted = await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
          ?.areNotificationsEnabled() ??
          false;


      XLogUtils.printLog('---granted=$granted');


    }

    if (XFlutterUtils.isiOS()||XFlutterUtils.isMacOS()) {
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
          IOSFlutterLocalNotificationsPlugin>()
          ?.requestPermissions(
        alert: true,
        badge: true,
        sound: true,
        critical: true,
      );
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
          MacOSFlutterLocalNotificationsPlugin>()
          ?.requestPermissions(
        alert: true,
        badge: true,
        sound: true,
        critical: true,
      );

    } else if (XFlutterUtils.isAndroid()) {
      final AndroidFlutterLocalNotificationsPlugin? androidImplementation =
      flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>();

      final bool? grantedNotificationPermission = await androidImplementation?.requestNotificationsPermission();

      XLogUtils.printLog('---grantedNotificationPermission=$grantedNotificationPermission');
    }





  }


  static void showBasicNotification() {
    // const AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
    //   'default_channel', // 通道ID
    //   '默认频道',         // 通道名称
    //   importance: Importance.max, // 通知重要性
    //   priority: Priority.high,    // 优先级
    //   showWhen: true,             // 显示时间戳
    // );
    //
    // const DarwinNotificationDetails iosDetails = DarwinNotificationDetails(
    //   presentAlert: true, // 显示弹窗
    //   presentBadge: true, // 显示角标
    //   presentSound: true, // 播放声音
    // );
    //
    // const NotificationDetails platformDetails = NotificationDetails(
    //   android: androidDetails,
    //   iOS: iosDetails,
    // );
    //
    // flutterLocalNotificationsPlugin.show(
    //   0,                     // 通知ID（唯一标识）
    //   '你好！',               // 标题
    //   '这是一条测试通知',      // 内容
    //   platformDetails,
    //   payload: 'notification_payload', // 点击通知时传递的数据
    // );

    const AndroidNotificationDetails androidNotificationDetails =
    AndroidNotificationDetails('1', 'your channel name',
        channelDescription: 'your channel description',
        importance: Importance.max,
        priority: Priority.high,
        ticker: 'ticker');
    const NotificationDetails notificationDetails =
    NotificationDetails(android: androidNotificationDetails);
     flutterLocalNotificationsPlugin.show(
        1, 'plain title', 'plain body', notificationDetails,
        payload: 'item x');
  }



}
